﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using StateGrid;
using StateGrid.User;
using StateGrid.GlobalVariables;
using StateGrid.WWHY;

public partial class AddSYML : System.Web.UI.Page
{
    /// <summary>
    /// 中文会议操作类
    /// </summary>
    private WWHY_DAO wwhyDAO = new WWHY_DAO();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!UserManageDAO.IsLogin((User)Session["login"]))
        {
            GlobalMethod.RedirectLogin(Response);
            return;
        }
        if (!this.IsPostBack)
        {
            this.BindData();
        }  
    }

    /// <summary>
    /// 绑定数据
    /// </summary>
    private void BindData()
    {
        //获取分类编码参数
        this.flbmTextBox.Text = Request["flbm"].ToString();
        if (null == this.flbmTextBox.Text || "".Equals(this.flbmTextBox.Text.Trim()))
        {
            String message = "对不起，没有找到对应的分类编码";
            Response.Redirect(GlobalResource.ERROR_PAGE+"?message="+message);
            return;
        }

        //获取分类编码
        WWHY_FL wwhyFL = wwhyDAO.getWwhyFLByFlbm(this.flbmTextBox.Text.Trim());

        if (null == wwhyFL)
        {
            String message = "对不起，没有找到对应的分类编码";
            Response.Redirect(GlobalResource.ERROR_PAGE + "?message=" + message);
            return;
        }

        //分类名称
        this.flmcTextBox.Text = wwhyFL.flmc;

        //索引编码
        this.sybmTextBox.Text = wwhyFL.flbm;

        //文种
        this.wzTextBox.Text = "";
        //年份
        this.nfTextBox.Text = "";
        //期号
        this.qhTextBox.Text = "";
        //中文名称
        this.zwmcTextBox.Text = "";
        //外文名称
        this.wwmcTextBox.Text = "";
        //主办单位
        this.zbdwTextBox.Text = "";
        //承办单位
        this.cbdwTextBox.Text = "";
        //出版单位
        this.chbdwTextBox.Text = "";
        //发布日期
        this.fbrqTextBox.Text = "";
        //会议地点
        this.hyddTextBox.Text = "";
        //简介
        this.jjTextBox.Text = "";
        //馆藏号
        this.gchTextBox.Text = "";
        //国图分类
        this.gtflTextBox.Text = "";
    }

    /// <summary>
    /// 提交
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void submitButton_Click(object sender, EventArgs e)
    {
        if (!this.NFValidate())
        {
            return;
        }
        if (!this.QHValidate())
        {
            return;
        }
        if (!this.SYBMValidate())
        {
            //Response.Write("<Script>alert('添加期刊索引失败')</Script>");
            return;
        }
        if (!this.WWMCValidate())
        {
            return;
        }
        if (!this.FBRQValidate())
        {
            return;
        }

        WWHY_SYML wwhySYML = this.GetWWHY_SYML();
        //添加索引编码
        this.wwhyDAO.insertWwhySYML(wwhySYML);

        Response.Write("<Script>alert('添加外文会议索引信息成功')</Script>");

        this.BindData();
       

        //this.Response.Redirect(GlobalChineseJournal.LISTCHINESEJOURNALCATALOG_PAGE + "?currentCatalog=" + this.sybmHiddenField.Value);

    }    

    /// <summary>
    /// 获取页面中的信息
    /// </summary>
    /// <returns>一个索引目录对象</returns>
    private WWHY_SYML GetWWHY_SYML()
    {
        WWHY_SYML wwhySYML = new WWHY_SYML();
        
        //分类编码
        wwhySYML.flbm = this.flbmTextBox.Text.Trim();
        //索引编码
        wwhySYML.sybm = this.sybmTextBox.Text.Trim();

        //文种
        wwhySYML.wz = this.wzTextBox.Text.Trim();
        //年份
        try
        {
            wwhySYML.nf = Int32.Parse(this.nfTextBox.Text.Trim());
        }
        catch (Exception)
        {
        }

        //中文名称
        wwhySYML.zwmc = this.zwmcTextBox.Text.Trim();
        //外文名称
        wwhySYML.wwmc = this.wwmcTextBox.Text.Trim();
        
        //主办单位
        wwhySYML.zbdw=this.zbdwTextBox.Text.Trim();
        //承办单位
        wwhySYML.cbdw= this.cbdwTextBox.Text.Trim();
        //出版单位
        wwhySYML.chbdw=this.chbdwTextBox.Text.Trim();
        try
        {
            //发布日期
            wwhySYML.fbrq = DateTime.Parse(this.fbrqTextBox.Text.Trim());
        }
        catch (Exception)
        {
        }
        //会议地点
        wwhySYML.hydd=this.hyddTextBox.Text.Trim();
        //简介
        wwhySYML.jj=this.jjTextBox.Text.Trim();
        //馆藏号
        wwhySYML.gch=this.gchTextBox.Text.Trim();
        //国图分类
        wwhySYML.gtfl = this.gtflTextBox.Text.Trim();
        //登录日期
        wwhySYML.dlrq = DateTime.Now;

        return wwhySYML;
    }

    /// <summary>
    /// 重新绑定数据
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void resetButton_Click(object sender, EventArgs e)
    {
        this.BindData();
    }
    protected void nfTextBox_TextChanged(object sender, EventArgs e)
    {
        this.nfTextBox.Text=this.nfTextBox.Text.Trim();
        this.qhTextBox.Text = "";
        if (this.NFValidate())
        {
            this.sybmTextBox.Text = this.flbmTextBox.Text + '-' + this.nfTextBox.Text;
        }
        else
        {
            this.nfTextBox.Text = "";
            this.sybmTextBox.Text = this.flbmTextBox.Text ;
        }
    }
    protected void qhTextBox_TextChanged(object sender, EventArgs e)
    {
        if (null == this.nfTextBox.Text || "".Equals(this.nfTextBox.Text))
        {
            this.qhTextBox.Text = "";
            Response.Write("<script>alert('请先输入年份，再输入期号')</script>");
        }
        else
        {
            if (this.QHValidate())
            {
                this.sybmTextBox.Text = this.flbmTextBox.Text + '-' + this.nfTextBox.Text + '-' + this.qhTextBox.Text;
            }
            else
            {
                this.sybmTextBox.Text = this.flbmTextBox.Text + '-' + this.nfTextBox.Text;
                this.qhTextBox.Text = "";
            }
        }
    }
    protected void fbrqTextBox_TextChanged(object sender, EventArgs e)
    {
        this.fbrqTextBox.Text = this.fbrqTextBox.Text.Trim();
        this.FBRQValidate();
    }
    
    /// <summary>
    /// 验证索引编码
    /// </summary>
    /// <returns></returns>
    private Boolean SYBMValidate()
    {
        Boolean flag = true;
        //清空索引编码信息
        this.errorLabel.Text = "";

        this.sybmTextBox.Text = this.sybmTextBox.Text.Trim();

        WWHY_SYML wwhySYML = wwhyDAO.getWwhySYMLBySybm(this.sybmTextBox.Text);
        if (null != wwhySYML)
        {
            this.errorLabel.Text = "错误：该分类编码已存在";
            flag = false;
        }
        return flag;
    }

    /// <summary>
    /// 验证中文名称
    /// </summary>
    /// <returns></returns>
    private Boolean WWMCValidate()
    {
        Boolean flag = true;
        //清空外文名称错误信息
        this.errorLabel.Text = "";
        if (null == this.wwmcTextBox.Text || "".Equals(this.wwmcTextBox.Text.Trim()))
        {
            this.errorLabel.Text = "错误:外文名称不能为空";
            flag = false;
        }
        return flag;
    }
    /// <summary>
    /// 验证年份
    /// </summary>
    /// <returns></returns>
    private Boolean NFValidate()
    {
        Boolean flag = true;
        this.errorLabel.Text = "";
        if (null == nfTextBox.Text || "".Equals(nfTextBox.Text))
        {
            flag = false;
            this.errorLabel.Text = "错误：年份不能为空";
        }

        if (this.nfTextBox.Text.Length != 4)
        {
            flag = false;
            this.errorLabel.Text = "错误：年份输入有误，请输入一个4位数";
        }
        else
        {
            try
            {
                int nf = Int32.Parse(this.nfTextBox.Text);
                if (nf < 1800 || nf > 9999)
                {
                    this.errorLabel.Text = "错误：请输入正确的年份，年份区间1000~9999";
                    flag = false;
                }
            }
            catch (Exception)
            {
                this.errorLabel.Text = "错误：请输入正确的年份";
                flag = false;
            }
        }
        return flag;
    }

    /// <summary>
    /// 验证期号
    /// </summary>
    /// <returns></returns>
    private Boolean QHValidate()
    {
        Boolean flag = true;
        this.errorLabel.Text = "";
        if (null == qhTextBox.Text || "".Equals(qhTextBox.Text))
        {
            flag = false;
            this.errorLabel.Text = "错误：期号不能为空";
        }

        if (this.qhTextBox.Text.Length != 3)
        {
            flag = false;
            this.errorLabel.Text = "错误：期号输入有误，请输入一个3位数";
        }
        else
        {
            try
            {
                int nf = Int32.Parse(this.qhTextBox.Text);
            }
            catch (Exception)
            {
                this.errorLabel.Text = "错误：请输入正确的期号，如：001";
                flag = false;
            }
        }
        return flag;
    }

    /// <summary>
    /// 验证发布日期
    /// </summary>
    /// <returns></returns>
    private Boolean FBRQValidate()
    {
        Boolean flag = true;
        this.errorLabel.Text = "";
        if (null == nfTextBox.Text || "".Equals(nfTextBox.Text))
        {
            return flag;
        }

        try
        {
            DateTime fbrq = DateTime.Parse(this.fbrqTextBox.Text);
            if (fbrq.Year<1000 || fbrq.Year > 9999)
            {
                this.errorLabel.Text = "错误：请输入正确的发布日期,年份在1000~9999之间";
                flag = false;
            }
        }
        catch (Exception)
        {
            this.errorLabel.Text = "错误：请输入正确的发布日期";
            flag = false;
        }
        return flag;
    }
   
}